<h2>Physics Tutorial 2: Pendulum</h2>
<p>
	In this tutorial, we set up a pendulum simulation in which a rectangle pendulum
	swings freely from a circular anchor under the force of gravity. The pendulum
	is attached to the anchor using a pin joint. The pin joint is a type of constraint
	- something that restricts the motion of the objects it affects.
</p>
<p>
	This tutorial teaches:
	<ul>
		<li>How to build a basic simulation using constraints</li>
		<li>How to attach two objects using a pin joint</li>
		<li>How to register a constraint with the physics engine</li>
		<li>How to apply a force to an object</li>
	</ul>
</p>

<h2>Create the Anchor</h2>
<p>
	In the <b>sim/app/physicsTutorial2</b> directory, create a file called <b>Anchor.java</b>.
	In this file add:
</p>
<pre>

package sim.app.physicsTutorial2;

import java.awt.*;
import sim.physics2D.physicalObject.*;
import sim.physics2D.util.*;

public class Anchor extends StationaryObject2D 
{
	public double radius;
	public Anchor(Double2D pos, double radius)
	{
		this.setPose(pos, new Angle(0));
		this.setShape(new sim.physics2D.shape.Circle(radius, Color.gray));
		this.setCoefficientOfRestitution(1);
	} 
}
</pre>
<p>
	The anchor is simply a stationary object (recall that a <b>StationaryObject</b>
	has infinite mass, so doesn't move no matter what) in the shape of a circle.
</p>
<h2>Create the Pendulum</h2>
<p>
	In the <b>sim/app/physicsTutorial2</b> directory, create a file called <b>Pendulum.java</b>.
	In this file add:
</p>
<pre>
package sim.app.physicsTutorial2;

import java.awt.*;
import sim.engine.*;
import sim.physics2D.physicalObject.*;
import sim.physics2D.util.*;
import sim.physics2D.forceGenerator.*;

public class Pendulum extends MobileObject2D implements Steppable, ForceGenerator
{
	public Pendulum(Double2D pos, Double2D vel, double width, double height, Paint paint)
	{
		this.setVelocity(vel);
		this.setPose(pos, new Angle(0));

		this.setShape(new sim.physics2D.shape.Rectangle(width, height, paint), width * height);

		this.setCoefficientOfFriction(0);
		this.setCoefficientOfRestitution(1);
	}
 
	public void step(SimState state)
	{
		Double2D position = this.getPosition();
		PhysicsTutorial2 simPhysicsTutorial2 = (PhysicsTutorial2)state;
		simPhysicsTutorial2.fieldEnvironment.setObjectLocation(this, new sim.util.Double2D(position.x, position.y));
	}
	
	public void addForce()
	{
		this.addForce(new Double2D(0, 5));
	}
}
</pre>
<p>
	The <b>Pendulum</b> is a rectangular mobile object. The only real difference
	between the <b>Pendulum</b> and the <b>MobilePoly</b> from physics tutorial 1 is
	that a force is applied to the pendulum (simulated gravity). Forces can be applied
	to mobile objects using <b>MobileObject</b>'s <b>addForce</b> method. This method
	takes as a parameter a vector (represented by the physics engine's <b>Double2D</b>) 
	defining the direction and magnitude of the force.
</p>
<p>
	Constant forces (like gravity) need to be applied at every time step throughout
	the simulation. Experienced MASON users may wonder, therefore, why the gravity 
	force isn't applied to the pendulum in the <b>step</b> method. The reason is that
	depending on the numerical integrator used, the force may need to be applied more
	than once per time step. For example, the default integrator provided with the 
	physics engine, the Runge-Kutta integrator, applies forces to objects four
	times in each time step. The physics engine deals with this issue by requiring anything that applies forces
	to an object to implement the <b>ForceGenerator</b> interface which exposes one method: 
	<b>addForce</b> (no parameters). In this case, the <b>Pendulum</b> applies a force to
	itself (to simulate gravity), so it implements the <b>ForceGenerator</b> interface
	and applies the gravity force within the <b>addForce</b> method.
</p>
<h2>Create the anchor and pendulum and constrain them together with a pin joint</h2>
<p>
	The file <b>sim/app/physicsTutorial2/PhysicsTutorial2.java</b> already has much of 
	the non-physics related MASON code needed to set up the simulation. The only code
	to add is in the <b>start</b> method after the "Add physics specific code here" line.
</p>
<pre>
		// Create and schedule the physics engine
		PhysicsEngine2D objPE = new PhysicsEngine2D();
		schedule.scheduleRepeating(objPE);
		
		// Create the anchor that the pendulum swings from
		Anchor anchor = new Anchor(new Double2D(100, 50), 5);
		fieldEnvironment.setObjectLocation(anchor, new sim.util.Double2D(100, 50));
		objPE.register(anchor);
		
		// Create the pendulum
		Pendulum pend = new Pendulum(new Double2D(80, 50), new Double2D(0, 0), 20, 5, Color.red);
		fieldEnvironment.setObjectLocation(pend, new sim.util.Double2D(80, 50));
		schedule.scheduleRepeating(pend);
		objPE.register(pend);
		
		// Connect the pendulum to the anchor with a pin joint
		PinJoint pj = new PinJoint(new Double2D(100, 50), anchor, pend);
		objPE.register(pj);
</pre>
<p>
	As in physics tutorial 1, this code creates and schedules the physics engine
	and the physical objects and registers the physical objects with 
	the physics engine. The last two lines create a pin joint that connects the anchor and 
	the pendulum. The <b>PinJoint</b> constructor takes three parameters - the initial position of the pin
	joint and the two objects to connect. In this case, we set the pin joint to have the same
	position as the anchor so the pendulum will swing around the center of the anchor.
</p>
<h2>Run the simulation</h2>
<p>
	Compile Anchor.java, Pendulum.java, PhysicsTutorial2.java, and PhysicsTutorial2WithUI.java. Then run the program 
	as <b>java sim.app.physicsTutorial2.PhysicsTutorial2WithUI</b>
</p>
<p>
	After pressing play, you should see a red rectangular pendulum swinging from 
	a gray circular anchor.
</p>

